﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using QuanLyChoVay.Database;
using QuanLyChoVay.Entity;
using QuanLyChoVay.Bussiness;
namespace QuanLyChoVay
{
    public partial class DangKyVay : Form
    {
        private SendData sd;
        public DangKyVay(SendData sd)
        {
            this.sd = sd;
            InitializeComponent();
            DangKyVayLoad();
        }
        public void DangKyVayLoad()
        {
            string tendn = sd.Tendangnhap;
            KhachHangBuss khb = new KhachHangBuss();
            DataTable dt = new DataTable();
            try
            {
                dt = khb.select_KhachHang_bytendn(tendn);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            txttendn.Text = dt.Rows[0][1].ToString();
            txtmatkhau.Text = dt.Rows[0][2].ToString();
            txthoten.Text = dt.Rows[0][3].ToString();
            txtemail.Text = dt.Rows[0][4].ToString();
            txtdiachi.Text = dt.Rows[0][5].ToString();
            dateTimePicker1.Text = dt.Rows[0][6].ToString();
            
            txtquequan.Text = dt.Rows[0][7].ToString();
            txtsdt.Text = dt.Rows[0][8].ToString();
            
            txttienluong.Text = dt.Rows[0][9].ToString();
            txttochuc.Text = dt.Rows[0][10].ToString();
            txttendn.Enabled = false;
            txtmatkhau.Enabled = false;

            btncapnhat.Visible = false;
            btnhuybo.Visible = false;
            //MessageBox.Show(tendn);
        }
        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void g_Load(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void btnthaydoithongtin_Click(object sender, EventArgs e)
        {
            string tendn = sd.Tendangnhap;
            KhachHangBuss khb = new KhachHangBuss();
            DataTable dt = new DataTable();
            try
            {
                dt = khb.select_KhachHang_bytendn(tendn);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            txttendn.Text = dt.Rows[0][1].ToString();
            txtmatkhau.Text = dt.Rows[0][2].ToString();
            txthoten.Text = dt.Rows[0][3].ToString();
            txtemail.Text = dt.Rows[0][4].ToString();
            txtdiachi.Text = dt.Rows[0][5].ToString();
            dateTimePicker1.Text = dt.Rows[0][6].ToString();

            txtquequan.Text = dt.Rows[0][7].ToString();
            txtsdt.Text = dt.Rows[0][8].ToString();

            txttienluong.Text = dt.Rows[0][9].ToString();
            txttochuc.Text = dt.Rows[0][10].ToString();

            txttendn.Enabled = false;
            txtmatkhau.Focus();
            btncapnhat.Visible = true;
            btnhuybo.Visible = true;
            btndangkyvay.Enabled = false;
            button2.Enabled = false;
            btnthaydoithongtin.Enabled = false;
            btnchitiettra.Enabled = false;


        }

        private void btncapnhat_Click(object sender, EventArgs e)
        {
            KhachHang kh = new KhachHang();
            kh.Tendn = txttendn.Text;
            kh.Matkhau = txtmatkhau.Text;
            kh.Hoten = txthoten.Text;
            kh.Email = txtemail.Text;
            kh.Diachi = txtdiachi.Text;
            kh.Ngaysinh = dateTimePicker1.Text;
            kh.Quequan = txtquequan.Text;
            kh.Sdt = txtsdt.Text;
            kh.Tienluong = Convert.ToInt32(txttienluong.Text);
            kh.Tochuc = txttochuc.Text;
            KhachHangBuss khb = new KhachHangBuss();
            try
            {
                khb.update_KhachHang_bytendn(kh);
                MessageBox.Show("cập nhật thành công");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                MessageBox.Show("Lỗi xảy ra khỉ cập nhật dữ liệu");
            }
            this.Hide();
            DangKyVay dkv =new DangKyVay(sd);
            dkv.Show();
        }

        private void btnhuybo_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn hủy bỏ cập nhật", "Xác nhận hủy bỏ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Hide();
                DangKyVay dkv = new DangKyVay(sd);
                dkv.ShowDialog();

            }
        }

        private void btndangkyvay_Click(object sender, EventArgs e)
        {
            //this.Hide();
            DangKyVayTiep dkvt = new DangKyVayTiep();
            dkvt.Show();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn thoát", "Xác nhận thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Hide();
                //Form1 main = new Form1();
                //main.ShowDialog();

            }
        }
    }
}
